import psutil
from psutil import AccessDenied, NoSuchProcess

from src.util.common_util import printx
from src.util.config_manager import ConfigManager


def process_statistic_entrance(config: ConfigManager) -> None:
    process = []
    for proc in psutil.process_iter(attrs=['pid', 'exe', 'open_files']):
        try:
            if proc.status() != psutil.STATUS_ZOMBIE:
                cmdline = proc.cmdline()
                if "python" in cmdline and "main.py" in cmdline:
                    process.append({'进程': proc.info['pid'], 'threads': proc.num_threads()})
        except (AccessDenied, NoSuchProcess):
            # 如果没有权限或进程不存在，跳过
            continue

    printx(f"一共:{len(process)}个进程，共:{sum([item['threads'] for item in process])}个线程")
    for i, proc in enumerate(process):
        printx(f"进程:{i:02d}有{proc['threads']}个线程")
